from sklearn.datasets import load_iris
from sklearn import tree
import sys
import os
from IPython.display import Image
import pydotplus
import pandas as pd
os.environ["PATH"] += os.pathsep + 'D:/Program Files (x86)/Graphviz2.38/bin/'
iris = load_iris()
table = pd.DataFrame(iris['data'])
table['target'] = iris['target']    # target => ['setosa', 'versicolor', 'virginica']
table.columns = ['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)', 'target']
print(iris.target_names)
print(iris.feature_names)
print(table)
# # 创建决策树对象，使用信息熵作为依据
# clf = tree.DecisionTreeClassifier(criterion='entropy')
# # fit方法分类。features为iris.data，labels为iris.target
# clf = clf.fit(iris.data, iris.target)
# # 可视化
# dot_data = tree.export_graphviz(clf, feature_names=iris.feature_names,  class_names=iris.target_names, filled=True)
# graph = pydotplus.graph_from_dot_data(dot_data)
# Image(graph.create_png())
# graph.write_png('tree.png')